package reflect;
import java.util.ArrayList;
import java.util.Scanner;
/**
 * 通过反射机制实例化对象
 */
public class ReflectDemo2 {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个类名：");
        String className = sc.nextLine();
        Class cls = Class.forName(className);
        /*
          通过newInstance()可以进行实例化
          假设cls为String对象，则newInstance()的结果相当于String s = new String();
          假设cls为ArrayList对象，则newInstance()的结果相当于ArrayList list = new ArrayList();
         */
        Object obj = cls.newInstance(); //只能使用无参构造器实例化！
        System.out.println(obj);
        /*
        ArrayList list = new ArrayList();   //java.util.ArrayList
        System.out.println(list);
         */
    }
}
